package com.m2dl.tpcsa.touchmycircle.common;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;

public class Circle extends View {

	private ShapeDrawable mDrawableInt;
	private ShapeDrawable mDrawableExt;
	private float positionXCenter = 100;
	private float positionYCenter = 100;
	private float maxScreenX = 500;
	private float maxScreenY = 500;
	private float internalRadius = 50;
	private float externalRadius = 100;

	private float coeffDifficulty = (float) 0.5;
	/** Number of circle shows during the game. */
	private int nbCircle;

	protected void onDraw(Canvas canvas) {
		canvas.drawCircle(this.positionXCenter, this.positionYCenter,
				this.internalRadius, mDrawableInt.getPaint());
		canvas.drawCircle(this.positionXCenter, this.positionYCenter,
				this.externalRadius, mDrawableExt.getPaint());
	}

	/** Super Constructor. */
	private void superConstructor() {
		this.nbCircle = 20;
		this.mDrawableInt = new ShapeDrawable(new OvalShape());
		this.mDrawableExt = new ShapeDrawable(new OvalShape());
		this.mDrawableInt.getPaint().setColor(Color.RED);
		this.mDrawableExt.getPaint().setColor(Color.RED);
		this.mDrawableExt.getPaint().setStyle(Style.STROKE);
		this.mDrawableExt.getPaint().setStrikeThruText(true);
		this.mDrawableExt.getPaint().setStrokeWidth(3);
	}

	/** Constructor. */
	public Circle(Context context) {
		super(context);
		superConstructor();
	}

	/** Constructor with attributeSet. */
	public Circle(Context context, AttributeSet attr) {
		super(context, attr);
		superConstructor();
	}

	public boolean changeCircle() {
		boolean finish = false;
		if(this.nbCircle > 0) {
			this.nbCircle--;
			this.externalRadius = 100;
			this.coeffDifficulty = (float) ((float) (Math.random() * (0.7)) + (0.3));
			this.positionXCenter = (int) (Math.random() * (this.maxScreenX)) + 100;
			this.positionYCenter = (int) (Math.random() * (this.maxScreenY)) + 100;
		} else {
			return true;
		}
		return finish;
	}

	/** Gets the NbCircle. */
	public int getNbCircle() {
		return nbCircle;
	}

	/**
	 * Sets the number circle to the game.
	 * 
	 * @param nbCircle
	 *            the number of circle
	 */
	public void setNbCircle(int nbCircle) {
		this.nbCircle = nbCircle;
	}

	/**
	 * Gets the circle center X position.
	 */
	public float getPositionXCenter() {
		return this.positionXCenter;
	}

	/**
	 * Gets the circle center Y position.
	 */
	public float getPositionYCenter() {
		return this.positionYCenter;
	}

	/**
	 * Gets the external radius.
	 * 
	 * @return the external radius
	 */
	public float getExternalRadius() {
		return externalRadius;
	}

	/**
	 * Sets the maximum seize of the screen.
	 * 
	 * @param theMaxX
	 *            screen height
	 * @param theMaxY
	 *            screen width
	 */
	public void setMaxScreen(float theMaxX, float theMaxY) {
		this.maxScreenX = theMaxX;
		this.maxScreenY = theMaxY;
	}

	/**
	 * Reduces the circle radius with the application handler.
	 */
	public boolean reduceExternalRadius() {
		if (this.externalRadius > this.internalRadius) {
			this.externalRadius -= this.coeffDifficulty;
		} else {
			return changeCircle();
		}
		return false;
	}

	/**
	 * Action performed with OnTouch event.
	 */
	public int onTouchAction(float eventX, float eventY) {
		// Checks if the user touch in the circle.
		double newXSquared = Math.pow(this.positionXCenter - eventX, 2);
		double newYSquared = Math.pow(this.positionYCenter - eventY, 2);
		float newRadius = (float) Math.sqrt(newXSquared + newYSquared);

		if (newRadius < this.externalRadius) {
			if (this.nbCircle > 0) {
				changeCircle();
			}
			return (int) this.externalRadius;
		}
		return 0;
	}
}